var app = getApp();
const quote = app.globalData.quote;
const path = quote.paths
const api = quote.api;
const LoginRegist = quote.LoginRegist;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    path:path,
    phone: null,
    phoneCode:null,
    getCode_ing:false,
    remainTime:null,
    isRegist:false,
    IsVip:null
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  //验证码获取输入
  bindblurPhoneCode:function(e){
    var that = this
    console.log(e)
    that.setData({
      phoneCode: e.detail.value
    })
  },

  //手机号码获取输入
  bindblurPhone: function (e) {
    var that=this
    console.log(e)
    that.setData({
      phone:e.detail.value
    })
  
    // LoginRegist.bindInput(e);
  },
  tapGetAuthCode: function (params, cb){
    var that=this
    var dataDic = {
      Phone: that.data.phone,
    }
    //判断是否为注册用户
    api.methodEnglishCorner_LoginVerificationCode({
      dataDic: dataDic,
      complete: function (res) {
        console.log(res.data)
        if (res.data.code == -4) {
          console.log('该用户还没注册')
          that.setData({
            isRegist:false
          })
          wx.showModal({
            title: '你还不是阿卡索用户',
            content: '成为阿卡索用户，立即获得专属外教一对一体验课，还可以成为VIP',
            cancelText:'再想想',
            success:function(res){
              if(res.confirm){
                //走注册流程
                //1.先发送验证码
                var dataDic = {
                  Phone: that.data.phone,
                }
                api.methodEnglishCorner_RegisterVerificationCode({
                  dataDic: dataDic,
                  success: function (res) {
                    console.log(res)
                    that.setData({
                      verification: res.data.data.code
                    })
                    if (that.data.verification != 0) {
                      console.log("不调用60秒倒计时函数")
                    } else {
                      that.countdownTime();
                    }

                    if (res.data.code != 0) {
                      wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg,
                      });
                      return;
                    } else {
                      if (res.data.data.code != 0) {
                        wx.showModal({
                          title: '温馨提示',
                          showCancel: false,
                          content: res.data.data.msg,
                        })
                      } else {
                        wx.showToast({
                          title: '发送成功',
                        })
                      }
                    }
                  },
                  fail: (res) => {
                    console.log(res);
                    wx.showModal({
                      title: "温馨提示",
                      showCancel: false,
                      content: "获取验证码失败，请稍后重试！",
                    });
                  }
                })

              }else{
                //不去注册 吧用户手机号码保存起来
                var dataDic = {
                  Phone: that.data.phone,
                }
                api.methodEnglishCorner_Recording({
                   dataDic:dataDic,
                   complete:function(res){
                     if(res.data.code == 0){
                       console.log('手机号码保存成功')
                     }else{
                       console.log('手机号码保存失败')
                     }
                   }
                })
              }
            },
            fail:(res)=>{
              console.log(res)
            }
          })
          return;
        } else if (res.data.code == 0) {
          that.setData({
            isRegist: true
          })
          console.log('该用户已经注册，验证码发送成功')
          wx.showToast({
            title: '发送成功',
          })
          
        }else{
          wx.showModal({
            title: "温馨提示",
            showCancel: false,
            content: res.data.msg ,
          });
        }

        typeof cb == 'function' && cb(res)
      }
    })

  },

  //倒计时
    countdownTime: function () {
   var that=this
    let initTime = 60;

    that.setData({
      getCode_ing: true,
      remainTime: initTime
    })

    that.downTimer = setInterval(function () {
      initTime--;
      that.setData({
        remainTime: initTime
      })
      if (initTime <= 0) {
        clearInterval(that.downTimer);
        that.setData({
          getCode_ing: false
        })
      }
    }, 1000);

  },

  //去授权
  gotoAuthorize:function(){
    var that=this
    if(that.data.isRegist){
       that.Authorize()
    }else{
      that.Regist()
    }


  },
  //授权流程
  Authorize:function(){
    var that=this 
    var dataDic = {
      Phone: that.data.phone,
      Code:that.data.phoneCode,
      OpenId: app.globalData.openId
    }
    api.methodEnglishCorner_LoginByVerificationCode({
      dataDic:dataDic,
      success:function(res){
        console.log(res,'授权成功，前往授权页面')
        app.globalData.UCUID = res.data.data.UCUID
        app.globalData.IsVip = res.data.data.IsVip
        app.globalData.Gold = res.data.data.Gold
        // app.globalData.IsAuthorization = res.data.data.IsAuthorization
        app.globalData.IsAuthorization = true
        that.setData({
          IsVip: res.data.data.IsVip
        })
        wx.setStorage({
          key: 'UCUID',
          data: res.data.data.UCUID,
        })
        wx.setStorage({
          key: 'IsVip',
          data: res.data.data.IsVip,
        })
        // wx.setStorage({
        //   key: 'IsAuthorization',
        //   data: res.data.data.IsAuthorization,
        // })
        wx.setStorage({
          key: 'Gold',
          data: res.data.data.Gold,
        })
        wx.setStorage({
          key: 'IsAuthorization',
          data: true,
        })
        
        setTimeout(() => {
          wx.navigateTo({
            url: `/${path.authorizeResult}?IsVip=${that.data.IsVip}`,
          })
        }, 1500)
      },
      faill:function(res){
        console.log(res)
      },
    })
  },

  //注册流程
  Regist:function(){
    var that = this
    var dataDic = {
      Phone: that.data.phone,
      Code: that.data.phoneCode,
      UserName: app.globalData.userInfo.nickName,
      Openid: app.globalData.openId,
      UserPwd:'123456',
      Source:app.globalData.appSource
    }
    api.methodEnglishCorner_Register({
      dataDic:dataDic,
      success:function(res){
        console.log('注册成功',res)
        app.globalData.UCUID = res.data.data.UCUID
        app.globalData.IsVip = res.data.data.IsVip
        app.globalData.Gold = res.data.data.Gold
        // app.globalData.IsAuthorization = res.data.data.IsAuthorization
        app.globalData.IsAuthorization = true
        that.setData({
          IsVip: res.data.data.IsVip
        })
        wx.setStorage({
          key: 'UCUID',
          data: res.data.data.UCUID,
        })
        wx.setStorage({
          key: 'IsVip',
          data: res.data.data.IsVip,
        })
        // wx.setStorage({
        //   key: 'IsAuthorization',
        //   data: res.data.data.IsAuthorization,
        // })
        wx.setStorage({
          key: 'Gold',
          data: res.data.data.Gold,
        })
        wx.setStorage({
          key: 'IsAuthorization',
          data: true,
        })

        setTimeout(() => {
          wx.navigateTo({
            url: `/${path.authorizeResult}?IsVip=${that.data.IsVip}`,
          })
        }, 1500)
      },
      fail:function(res){
        console.log('注册失败',res)
      }
    })

  }






})